package com.sws.hm.controller;


import cn.hutool.core.util.PageUtil;
import com.sws.common.BaseResponse;
import com.sws.common.StatusCode;
import com.sws.hm.dao.EmailDao;
import com.sws.hm.dao.portDao;
import com.sws.hm.dto.email;
import com.sws.hm.dto.port;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/email")
@CrossOrigin
@Slf4j
public class emailController {
    @Autowired
    private EmailDao dao;

    @RequestMapping("findAll")
    public BaseResponse findAll(String name) {

        List<email> data=dao.findAll(name);
        int count=data.size();
        BaseResponse response=new BaseResponse(StatusCode.Success);
        response.setCount(count);
        response.setData(data);
        return response;
    }

    @PostMapping("add")
    public BaseResponse add(String name,String email) {
        int num=dao.insertEmail(email,name);
        BaseResponse response=new BaseResponse(StatusCode.Success);
        response.setCount(num);
        return response;
    }

    @PostMapping("deleteById")
    public BaseResponse delete(int id) {
        int num=dao.deleteById(id);
        BaseResponse response=new BaseResponse(StatusCode.Success);
        response.setCount(num);
        return response;
    }

    @RequestMapping("UpdatePort")
    public BaseResponse UpdatePort(HttpServletRequest request) {
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String email=request.getParameter("email");
        int num=dao.updateEmail(email,name,id);
        BaseResponse response=new BaseResponse(StatusCode.Success);
        response.setCount(num);
        return response;
    }
}
